<template>
  <div class="personal-main">
    <el-table :data="borrowInfos" stripe style="width: 100%">
      <el-table-column prop="amount" label="借款金额"> </el-table-column>
      <el-table-column prop="period" label="期数"> </el-table-column>
      <el-table-column prop="borrowYearRate" label="年化率"> </el-table-column>
      <el-table-column prop="param.returnMethod" label="还款方式">
      </el-table-column>
      <el-table-column prop="param.moneyUse" label="资金用途">
      </el-table-column>
      <el-table-column prop="createTime" label="申请时间"> </el-table-column>
      <el-table-column prop="param.status" label="状态"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.status === 1"
            @click="handleCancel(scope.$index, scope.row)"
          >
            撤销申请
          </el-button>
          <!--<el-tag size="mini">{{ scope.row.param.status }}</el-tag>-->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pageNum"
      :page-size="pageSize"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[2,1, 10, 20]"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 3,
      borrowInfos: [],
      total: 0,
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    handleSizeChange(size) {
      this.pageSize = size;
      this.initData();
    },
    handleCurrentChange(num) {
      this.pageNum = num;
      this.initData();
    },
    handleCancel(idx, row) {
      this.$confirm("此操作将撤销借款记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let token = localStorage.getItem("FINANCE-TOKEN");
          if (!token) {
            this.$message.error("请先登录");
            return;
          }
          this.$axios({
            url: `/core/api/borrowInfo/my/cancel/${row.id}`,
            method: "PUT",
            headers: { token },
          }).then((r) => {
            this.$message.success(r.data.message);
            this.initData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    initData() {
      let token = localStorage.getItem("FINANCE-TOKEN");
      if (!token) {
        this.$message.error("请先登录");
        return;
      }
      this.$axios({
        url: `/core/api/borrowInfo/my/${this.pageNum}/${this.pageSize}`,
        method: "GET",
        headers: { token },
      }).then((response) => {
        console.log(response.data.data);
        this.borrowInfos = response.data.data.records
        this.total = Number(response.data.data.total);
      });
    },
  },
};
</script>
